using AutoMapper;
using KayakMes.Common;
using KayakMes.Common.Dtos.TeamDTO.Input;
using KayakMes.Common.Dtos.TeamDTO.Output;
using KayakMes.Common.Result;
using KayakMes.ProcessManagement.Domain.SchedulingModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.ProcessManagement.Application.Scheduling
{
    public class TeamTypeService : ITeamTypeService
    {
        private readonly BaseReposiotry<TeamTypeModel> _teamTypeRepository;
        private readonly IMapper _mapper;

        public TeamTypeService(BaseReposiotry<TeamTypeModel> teamTypeRepository, IMapper mapper)
        {
            _teamTypeRepository = teamTypeRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增班组类型
        /// </summary>
        /// <param name="input">班组类型输入信息</param>
        /// <returns></returns>
        public ApiResult<int> Add(TeamTypeInput input)
        {
            var entity = _mapper.Map<TeamTypeModel>(input);
            entity.Id = YitIdHelper.NextId();
            entity.CreateTime = DateTime.Now;

            var res = _teamTypeRepository.Add(entity);
            if (res > 0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加班组类型成功",
                    Data = res
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加班组类型失败",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 获取班组类型列表（分页）
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        public ApiPaging<TeamTypeOutput> GetTeamTypeList(GetTeamTypeInput input)
        {
            var query = _teamTypeRepository.GetAll().Where(x => !x.IsDeleted);

            if (!string.IsNullOrEmpty(input.TeamTypeName))
            {
                query = query.Where(x => x.TeamTypeCode.Contains(input.TeamTypeName));
            }

            if (!string.IsNullOrEmpty(input.CreateUser))
            {
                query = query.Where(x => x.CreateUser.Contains(input.CreateUser));
            }

            if (input.CreateTime != null)
            {
                query = query.Where(x => x.CreateTime>=input.CreateTime.Value.AddDays(-1) && x.CreateTime<input.CreateTime.Value.AddDays(1));
            }

            // 计算总记录数
            int total = query.Count();

            // 获取分页参数
            int pageIndex = input.PageIndex;
            int pageSize = input.PageSize;

            // 执行分页查询
            var list = query.OrderBy(x => x.Id)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            // 使用AutoMapper映射为输出DTO
            var entity = _mapper.Map<List<TeamTypeOutput>>(list);

            return new ApiPaging<TeamTypeOutput>()
            {
                Total = total,
                PageIndex = pageIndex,
                PageSize = pageSize,
                Items = entity,
                Code = ApiEnums.Success,
                Message = "获取班组类型列表成功"
            };
        }

        /// <summary>
        /// 根据Id获取班组类型信息
        /// </summary>
        /// <param name="id">班组类型ID</param>
        /// <returns></returns>
        public ApiResult<TeamTypeOutput> FindTeamType(long id)
        {
            var query = _teamTypeRepository.GetModel(id);

            if (query == null)
            {
                return new ApiResult<TeamTypeOutput>()
                {
                    Code = ApiEnums.Fail,
                    Message = "未找到指定的班组类型",
                    Data = null
                };
            }

            var entity = _mapper.Map<TeamTypeOutput>(query);

            return new ApiResult<TeamTypeOutput>()
            {
                Code = ApiEnums.Success,
                Data = entity,
                Message = "查询班组类型成功"
            };
        }

        /// <summary>
        /// 根据Id删除班组类型
        /// </summary>
        /// <param name="id">班组类型ID</param>
        /// <returns></returns>
        public ApiResult<int> DeleteTeamType(long id)
        {
            var query = _teamTypeRepository.GetModel(id);
            if (query != null && id > 0)
            {
                var res = _teamTypeRepository.Delete(query);

                if (res > 0)
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Success,
                        Data = res,
                        Message = "删除班组类型成功"
                    };
                }
                else
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Data = res,
                        Message = "删除班组类型失败"
                    };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Data = -1,
                    Message = "找不到要删除的班组类型！"
                };
            }
        }

        /// <summary>
        /// 修改班组类型信息
        /// </summary>
        /// <param name="input">班组类型更新信息</param>
        /// <returns></returns>
        public ApiResult<int> UpdateTeamType(TeamTypeOutput input)
        {
            var query = _teamTypeRepository.GetModel(input.Id);

            if (query != null && input.Id > 0)
            {
                var entity = _mapper.Map(input,query);

                var res = _teamTypeRepository.Update(entity);

                if (res > 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Message = "更新班组类型成功",
                        Data = res
                    };
                }
                else
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Data = res,
                        Message = "更新班组类型失败"
                    };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Data = -1,
                    Message = "找不到要修改的班组类型！"
                };
            }
        }
    }
} 